/*
 * @Description:
 * @Date: 2023-02-16 16:53:48
 */
import { createRouter, createWebHistory } from 'vue-router'
import BasicLayout from '../layouts/BasicLayout'
import Login from '../views/Login'
import SecurityLayout from '../layouts/SecurityLayout'
import microRoutersAuth from './microRoutersAuth'
import microRoutersLawsuit from './microRoutersLawsuit'
import store from 'store'

// 主应用路由注册
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      meta: { public: true }, // public 是否可以无权限访问
      component: Login
    },
    {
      path: '/',
      name: 'security',
      component: SecurityLayout,
      children: [
        {
          path: '/',
          name: 'layout',
          component: BasicLayout,
          children: [microRoutersAuth, microRoutersLawsuit]
        }
      ]
    }
  ]
})

// 路由守卫 需要权限页面不让访问
router.beforeEach((to, _, next) => {
  next();
  // //判断该路由是否需要登录权限
  // if (to.matched.some((record) => !record.meta.public)) {
  //   //获取token
  //   const sessionId = store.get('SESSION_ID')
  //   if (sessionId) {
  //     //退出登录、打开登录页
  //     if (to.path !== '/login') {
  //       next()
  //     }
  //   } else {
  //     console.log('未登录，守卫拦截')
  //     next({
  //       path: '/login'
  //     })
  //   }
  // } else {
  //   //直接访问
  //   next()
  // }
})

// 根据路由生成菜单
const formatMenu = () => {
  const concatMenu = [...microRoutersAuth.children, ...microRoutersLawsuit.children]

  return concatMenu
}

export { router, formatMenu }
